<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="编号" prop="id">
        <el-input v-model="queryParams.id"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择">
          <el-option label="初始" value="INIT" />
          <el-option label="完成" value="SUCCESS" />
          <el-option label="失败" value="FAIL" />
          <el-option label="处理中" value="ING" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="walletRechargeCashList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="id" show-overflow-tooltip />
      <el-table-column label="金额" align="center" prop="amount" width="80px" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="状态" align="center" prop="statusLabel">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="danger">{{ scope.row.statusLabel }}</el-tag>
          <el-tag v-else>{{ scope.row.statusLabel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="充值图片" align="center" prop="imgUrl" min-width="100px">
        <template #default="scope">
          <el-image class="czimg" fit="fill" :src="scope.row.imgUrl" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="90">
        <template #default="scope">
          <div v-if="scope.row.status === '0'">
            <el-button link size="small" type="primary" @click="handleConfirm(scope.row)"
              v-hasPermi="['system:walletRechargeCash:confirm']">确认
            </el-button>
            <el-button link size="small" type="danger" @click="handleRefuse(scope.row)"
              v-hasPermi="['system:walletRechargeCash:refuse']">驳回
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size"
      @pagination="getList" />

    <!-- 驳回订单填入原因对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRefuseForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="新增提现" v-model="addShow" width="500px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="提现金额">
          <el-input-number :min="0" :max="detail.balance*1" v-model="addForm.amount"></el-input-number>
          <span style="margin-left: 10px;">当前可用余额：{{ detail.balance }}</span>
        </el-form-item>
        <el-form-item label="收款地址">
          <el-input v-model="addForm.cashAddress"></el-input>
        </el-form-item>


        <el-form-item label="收款图片">
          <el-image style="width: 150px;height: 150px;" fit="fill" :src="addForm.iamgeUrls" />
        </el-form-item>

        <el-form-item label="上传凭证">
          <image-upload v-model="addForm.imgUrl" :limit="1"></image-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addSubmit">确 定</el-button>
          <el-button @click="addCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WalletRechargeCash">
import { confirmScore, refuseScore } from "@/api/boot/walletRechargeCash";
import { walletTransList,createRecharge,getBoundUser } from "@/api/charge/index.js";
import {  getImg} from "@/api/system/user";

const { proxy } = getCurrentInstance();

const walletRechargeCashList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const addShow = ref(false)
const total = ref(0);
const title = ref("");
const detail = ref({})
const addForm = ref({
  channel: 'USDT',
  amount: '',
  imgUrl: '',
  cashAddress: '',
  type: `2`,
  iamgeUrls:''
})

const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    type: `2`
  },
  rules: {
    
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询充值提现流水列表 */
function getList() {
  loading.value = true;
  walletTransList(queryParams.value).then(res => {
    walletRechargeCashList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  })
}


function getImgs(){
  getImg(1).then(e=>{
    addForm.value.iamgeUrls = e.msg
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
//新增充值
function handleAdd() {
  addShow.value = true
}
function addSubmit() {
  createRecharge(addForm.value).then(res => {
    if(res.code == 200) {
      addForm.value.imgUrl = ''
      addShow.value = false
      proxy.$modal.msgSuccess("添加成功");
      getList();
    }
  })
}
function addCancel() {
  addShow.value = false
}
// 表单重置
function reset(row) {
  form.value = {
    id: row.id
  };
  proxy.resetForm("walletRechargeCashRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleConfirm(row) {
  proxy.$modal.confirm_tg('是否确认【' + row.type + '】订单"' + row.id + '"完成', '确认订单').then(function () {
    confirmScore(row.id).then(res => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess("操作成功");
        getList();
      }
    })
  }).catch(() => { })
}

function handleRefuse(row) {
  reset(row);
  title.value = '驳回订单'
  open.value = true;
}

function submitRefuseForm() {
  proxy.$refs["walletRechargeCashRef"].validate(valid => {
    if (valid) {
      refuseScore(form.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess("驳回成功");
          open.value = false;
          getList();
        }
      })
    }
  })
}
function getBalance() {
  getBoundUser().then(res => {
    detail.value = res.data
  })
}

getList();
getBalance();

getImgs();

</script>
<style>
.czimg {
  width: 30px;
  height: 30px;
}

.czimg:hover {
  width: 100px;
  height: 100px;
}
</style>